#include "modbus.h"
#include "datahandle.h"
#include <QDebug>

ModbusSlave::ModbusSlave(_MODBUS_TYPE Type, const QByteArray &data):m_MbType(Type),m_SrcByte(data)
{
    m_DataByte.clear();
    m_MbSlave=this->MbByteToMbSlave(m_SrcByte);
}

QByteArray ModbusSlave::RespondFuntionError()
{
    QByteArray RetByte;
    unsigned int CRC=0x00;
    switch (m_MbType)
    {
    case _MODBUS_TYPE::_MODBUSRTU:
        RetByte.append(m_SrcByte[0]);
        RetByte.append(0x80+m_SrcByte[1]);
        RetByte.append((char)0x01);
        CRC=CheckCRC_16(RetByte,0,RetByte.count());
        RetByte.append((CRC&0x00FF));
        RetByte.append((CRC&0xFF00)>>8);
        break;
    case _MODBUS_TYPE::_MODBUSTCP:
        RetByte=m_SrcByte.mid(0,4);
        RetByte.append((char)0x00);
        RetByte.append((char)0x03);
        RetByte.append(m_SrcByte[6]);
        RetByte.append(0x80+m_SrcByte[7]);
        RetByte.append((char)0x01);
        break;
    default:
        break;
    }
    return RetByte;
}

QByteArray ModbusSlave::RespondRegOverflow()
{
    QByteArray RetByte;
    unsigned int CRC=0x00;
    switch (m_MbType)
    {
    case _MODBUS_TYPE::_MODBUSRTU:
        RetByte.append(m_SrcByte[0]);
        RetByte.append(0x80+m_SrcByte[1]);
        RetByte.append((char)0x02);
        CRC=CheckCRC_16(RetByte,0,RetByte.count());
        RetByte.append((CRC&0x00FF));
        RetByte.append((CRC&0xFF00)>>8);
        break;
    case _MODBUS_TYPE::_MODBUSTCP:
        RetByte=m_SrcByte.mid(0,4);
        RetByte.append((char)0x00);
        RetByte.append((char)0x03);
        RetByte.append(m_SrcByte[6]);
        RetByte.append(0x80+m_SrcByte[7]);
        RetByte.append((char)0x02);
        break;
    default:
        break;
    }
    return RetByte;
}

QByteArray ModbusSlave::RespondWriteData()
{
    QByteArray ByteTmp;
    switch (m_MbSlave._FunCode) {
    case _FUNCTION_CODE::_CODE_05:
        return  m_SrcByte;
        break;
    case _FUNCTION_CODE::_CODE_06:
        return  m_SrcByte;
        break;
    case _FUNCTION_CODE::_CODE_10:
        ByteTmp.append(m_MbSlave._BegAddr_U);
        ByteTmp.append(m_MbSlave._BegAddr_L);
        ByteTmp.append((m_MbSlave._RegCount&0xFF00)>>8);
        ByteTmp.append((m_MbSlave._RegCount&0x00FF));
        break;
    case _FUNCTION_CODE::_CODE_0F:
        ByteTmp.append(m_MbSlave._BegAddr_U);
        ByteTmp.append(m_MbSlave._BegAddr_L);
        ByteTmp.append((m_MbSlave._RegCount&0xFF00)>>8);
        ByteTmp.append((m_MbSlave._RegCount&0x00FF));
        break;
    default:
        return  m_SrcByte;
        break;
    }
    QByteArray RetByte;
    if(m_MbType==_MODBUS_TYPE::_MODBUSTCP)
    {
        RetByte=m_SrcByte.mid(0,8);
        RetByte.append(ByteTmp);
    }
    else if(m_MbType==_MODBUS_TYPE::_MODBUSRTU)
    {
        RetByte=m_SrcByte.mid(0,2);
        RetByte.append(ByteTmp);
        unsigned int CRC=CheckCRC_16(RetByte,0,RetByte.count());
        RetByte.append(CRC&0x00FF);
        RetByte.append((CRC&0xFF00)>>8);
    }
    return RetByte;
}

QByteArray ModbusSlave::RespondReadData(const QByteArray &Respond)
{
    QByteArray RetByte;
    QByteArray fillTmp;
    int byteSum=m_MbSlave._RegCount*2;
    int TcpByteSum;
    if(Respond.count()<byteSum)//需要返回的长度大于实际长度时，默认填充0
    {
        fillTmp.fill(0x00,byteSum-Respond.count());
    }
    if(m_MbSlave._FunCode==_FUNCTION_CODE::_CODE_03||m_MbSlave._FunCode==_FUNCTION_CODE::_CODE_04)
    {
        if(m_MbType==_MODBUS_TYPE::_MODBUSTCP)
        {
            TcpByteSum=3+Respond.count()+fillTmp.count();
            RetByte=m_SrcByte.mid(0,4);
            RetByte.append((TcpByteSum&0xFF00)>>8);
            RetByte.append(TcpByteSum&0x00FF);
            RetByte.append(m_SrcByte.mid(6,2));
            RetByte.append(byteSum);
            RetByte.append(Respond);
            RetByte.append(fillTmp);
        }
        else if(m_MbType==_MODBUS_TYPE::_MODBUSRTU)
        {
            RetByte=m_SrcByte.mid(0,2);
            RetByte.append(byteSum);
            RetByte.append(Respond);
            RetByte.append(fillTmp);
            unsigned int CRC=CheckCRC_16(RetByte,0,RetByte.count());
            RetByte.append(CRC&0x00FF);
            RetByte.append((CRC&0xFF00)>>8);
            //qDebug()<<RetByte.toHex().toUpper()<<byteSum;
        }
    }
    return RetByte;
}

_MODBUS_SLAVE ModbusSlave::MbByteToMbSlave(const QByteArray &data)
{
    switch (m_MbType) {
    case _MODBUS_TYPE::_MODBUSRTU:
        return MbRtuByteToMbSlave(data);
        break;
    case _MODBUS_TYPE::_MODBUSTCP:
        return MbTcpByteToMbSlave(data);
        break;
    default:
        return _MODBUS_SLAVE();
        break;
    }
    return _MODBUS_SLAVE();
}

_MODBUS_SLAVE ModbusSlave::MbTcpByteToMbSlave(const QByteArray &data)
{
    int count=data.count();
    if(count<8){
        return _MODBUS_SLAVE();
    }
    unsigned char *MbRtu=(unsigned char *)data.mid(6,count-6).data();
    _MODBUS_SLAVE RetSlave;
    RetSlave._DevAddr=MbRtu[0];
    RetSlave._FunCode=(_FUNCTION_CODE)MbRtu[1];

    unsigned int EndRegAddr=0x00;
    if(RetSlave._FunCode==_FUNCTION_CODE::_CODE_01||RetSlave._FunCode==_FUNCTION_CODE::_CODE_02
            ||RetSlave._FunCode==_FUNCTION_CODE::_CODE_03||RetSlave._FunCode==_FUNCTION_CODE::_CODE_04)
    {
        if(count<12){
            return _MODBUS_SLAVE();
        }else{
            RetSlave._BegAddr_U=MbRtu[2];
            RetSlave._BegAddr_L=MbRtu[3];
            RetSlave._RegCount=(MbRtu[4]<<8)+MbRtu[5];
            EndRegAddr=(RetSlave._BegAddr_U<<8)+RetSlave._BegAddr_L+RetSlave._RegCount-1;
            RetSlave._EndAddr_U=(EndRegAddr&0xFF00)>>8;
            RetSlave._EndAddr_L=EndRegAddr&0x00FF;
            RetSlave._IsValid=true;
            return RetSlave;
        }
    }else if(RetSlave._FunCode==_FUNCTION_CODE::_CODE_05){
        if(count<12){
            return _MODBUS_SLAVE();
        }else{
            RetSlave._BegAddr_U=MbRtu[2];
            RetSlave._BegAddr_L=MbRtu[3];
            RetSlave._RegCount=1;
            RetSlave._EndAddr_U=RetSlave._BegAddr_U;
            RetSlave._EndAddr_L=RetSlave._BegAddr_L;
            RetSlave._IsValid=true;
            m_DataByte=data.mid(10,2);
            return RetSlave;
        }
    }else if(RetSlave._FunCode==_FUNCTION_CODE::_CODE_06){
        if(count<12){
            return _MODBUS_SLAVE();
        }else{
            RetSlave._BegAddr_U=MbRtu[2];
            RetSlave._BegAddr_L=MbRtu[3];
            RetSlave._RegCount=1;
            RetSlave._EndAddr_U=RetSlave._BegAddr_U;
            RetSlave._EndAddr_L=RetSlave._BegAddr_L;
            RetSlave._IsValid=true;
            m_DataByte=data.mid(10,2);
            return RetSlave;
        }
    }else if(RetSlave._FunCode==_FUNCTION_CODE::_CODE_10){
        if(count<12){
            return _MODBUS_SLAVE();
        }else{
            RetSlave._BegAddr_U=MbRtu[2];
            RetSlave._BegAddr_L=MbRtu[3];
            RetSlave._RegCount=(MbRtu[4]<<8)+MbRtu[5];
            EndRegAddr=(RetSlave._BegAddr_U<<8)+RetSlave._BegAddr_L+RetSlave._RegCount-1;
            RetSlave._EndAddr_U=(EndRegAddr&0xFF00)>>8;
            RetSlave._EndAddr_L=EndRegAddr&0x00FF;
            RetSlave._IsValid=true;
            m_DataByte=data.mid(13,RetSlave._RegCount*2);
            return RetSlave;
        }
    }else if(RetSlave._FunCode==_FUNCTION_CODE::_CODE_0F){
        if(count<12){
            return _MODBUS_SLAVE();
        }else{
            RetSlave._BegAddr_U=MbRtu[2];
            RetSlave._BegAddr_L=MbRtu[3];
            RetSlave._RegCount=(MbRtu[4]<<8)+MbRtu[5];
            EndRegAddr=(RetSlave._BegAddr_U<<8)+RetSlave._BegAddr_L+RetSlave._RegCount-1;
            RetSlave._EndAddr_U=(EndRegAddr&0xFF00)>>8;
            RetSlave._EndAddr_L=EndRegAddr&0x00FF;
            RetSlave._IsValid=true;
            m_DataByte=data.mid(13,RetSlave._RegCount*2);
            return RetSlave;
        }
    }
    return RetSlave;
}

_MODBUS_SLAVE ModbusSlave::MbRtuByteToMbSlave(const QByteArray &data)
{
    int count=data.count();
    if(count<8||CheckCRC_16_IsOk(data,0,count)==false){
        return _MODBUS_SLAVE();
    }
    unsigned char *MbRtu=(unsigned char *)data.data();
    _MODBUS_SLAVE RetSlave;
    RetSlave._DevAddr=MbRtu[0];
    RetSlave._FunCode=(_FUNCTION_CODE)MbRtu[1];

    unsigned int EndRegAddr=0x00;
    if(RetSlave._FunCode==_FUNCTION_CODE::_CODE_01||RetSlave._FunCode==_FUNCTION_CODE::_CODE_02
            ||RetSlave._FunCode==_FUNCTION_CODE::_CODE_03||RetSlave._FunCode==_FUNCTION_CODE::_CODE_04)
    {
        if(count<6){
            return _MODBUS_SLAVE();
        }else{
            RetSlave._BegAddr_U=MbRtu[2];
            RetSlave._BegAddr_L=MbRtu[3];
            RetSlave._RegCount=(MbRtu[4]<<8)+MbRtu[5];
            EndRegAddr=(RetSlave._BegAddr_U<<8)+RetSlave._BegAddr_L+RetSlave._RegCount-1;
            RetSlave._EndAddr_U=(EndRegAddr&0xFF00)>>8;
            RetSlave._EndAddr_L=EndRegAddr&0x00FF;
            RetSlave._IsValid=true;
            return RetSlave;
        }
    }else if(RetSlave._FunCode==_FUNCTION_CODE::_CODE_05){
        if(count<6){
            return _MODBUS_SLAVE();
        }else{
            RetSlave._BegAddr_U=MbRtu[2];
            RetSlave._BegAddr_L=MbRtu[3];
            RetSlave._RegCount=1;
            RetSlave._EndAddr_U=RetSlave._BegAddr_U;
            RetSlave._EndAddr_L=RetSlave._BegAddr_L;
            RetSlave._IsValid=true;
            m_DataByte=data.mid(4,2);
            return RetSlave;
        }
    }else if(RetSlave._FunCode==_FUNCTION_CODE::_CODE_06){
        if(count<6){
            return _MODBUS_SLAVE();
        }else{
            RetSlave._BegAddr_U=MbRtu[2];
            RetSlave._BegAddr_L=MbRtu[3];
            RetSlave._RegCount=1;
            RetSlave._EndAddr_U=RetSlave._BegAddr_U;
            RetSlave._EndAddr_L=RetSlave._BegAddr_L;
            RetSlave._IsValid=true;
            m_DataByte=data.mid(4,2);
            return RetSlave;
        }
    }else if(RetSlave._FunCode==_FUNCTION_CODE::_CODE_10){
        if(count<6){
            return _MODBUS_SLAVE();
        }else{
            RetSlave._BegAddr_U=MbRtu[2];
            RetSlave._BegAddr_L=MbRtu[3];
            RetSlave._RegCount=(MbRtu[4]<<8)+MbRtu[5];
            EndRegAddr=(RetSlave._BegAddr_U<<8)+RetSlave._BegAddr_L+RetSlave._RegCount-1;
            RetSlave._EndAddr_U=(EndRegAddr&0xFF00)>>8;
            RetSlave._EndAddr_L=EndRegAddr&0x00FF;
            RetSlave._IsValid=true;
            m_DataByte=data.mid(7,RetSlave._RegCount*2);
            return RetSlave;
        }
    }else if(RetSlave._FunCode==_FUNCTION_CODE::_CODE_0F){
        if(count<6){
            return _MODBUS_SLAVE();
        }else{
            RetSlave._BegAddr_U=MbRtu[2];
            RetSlave._BegAddr_L=MbRtu[3];
            RetSlave._RegCount=(MbRtu[4]<<8)+MbRtu[5];
            EndRegAddr=(RetSlave._BegAddr_U<<8)+RetSlave._BegAddr_L+RetSlave._RegCount-1;
            RetSlave._EndAddr_U=(EndRegAddr&0xFF00)>>8;
            RetSlave._EndAddr_L=EndRegAddr&0x00FF;
            RetSlave._IsValid=true;
            m_DataByte=data.mid(7,RetSlave._RegCount*2);
            return RetSlave;
        }
    }
    return RetSlave;
}

_MODBUS_SLAVE ModbusSlave::getMbSlave()
{
    return  m_MbSlave;
}

QByteArray ModbusSlave::getDataByte()
{
    return m_DataByte;
}

ModbusMaster::ModbusMaster(_MODBUS_TYPE Type, _FUNCTION_CODE code):m_MbType(Type),m_MbCode(code)
{
    m_Addr=1;
    m_PInt16[0]=0;
    m_PInt16[1]=1;

    m_PInt32[0]=0;
    m_PInt32[1]=1;
    m_PInt32[2]=2;
    m_PInt32[3]=3;

    m_PFloat[0]=0;
    m_PFloat[1]=1;
    m_PFloat[2]=2;
    m_PFloat[3]=3;
    this->clenData();
}

QByteArray ModbusMaster::SendDataToByte()
{
    if(m_Data.count()==0){ return QByteArray(); }
    QByteArray RetByte;
    if(m_MbType==_MODBUS_TYPE::_MODBUSTCP)
    {
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x06));
    }
    RetByte.append(char(m_Addr));
    RetByte.append(char(m_MbCode));
    RetByte.append(char((m_begRegigter&0xFF00)>>8));
    RetByte.append(char(m_begRegigter&0x00FF));
    RetByte.append(char((m_registerCoutn&0xFF00)>>8));
    RetByte.append(char(m_registerCoutn&0x00FF));
    if(m_MbType==_MODBUS_TYPE::_MODBUSRTU)
    {
        CheckCRC_16_add(RetByte,0,RetByte.count());
    }
    return RetByte;
}

int ModbusMaster::RespondByteRead(QByteArray &RespondByte)
{
    int ret=RespondByteIsValid(RespondByte,m_MbCode);
    if(ret!=0){ return ret; }

    unsigned char *CurrByte=(unsigned char *)RespondByte.data();
    int yushu=0;
    if(m_MbCode==_FUNCTION_CODE::_CODE_01||m_MbCode==_FUNCTION_CODE::_CODE_02)//开关量读取
    {
        int TmpRegister=0;
        int index=0;
        int tmpValue=0;
       // qDebug()<<endl;
        for(int i=0;i<m_Data.count();i++)
        {
            TmpRegister=m_Data[i]._Regisetr_U*256+m_Data[i]._Regisetr_L;
            index=(TmpRegister-m_begRegigter)/8+3;//确定数据在第几个字节,加个3偏移地址功，能码，长度字节
            yushu=(TmpRegister-m_begRegigter)%8;//确定数据在第几位
            tmpValue=(CurrByte[index]&(0x01<<yushu));
            if(tmpValue==0)
            {
                m_Data[i].m_bool=false;
            }else{
                m_Data[i].m_bool=true;
            }
           // qDebug()<<"================m_bool"<<m_Data[i].m_bool;
        }
    }
    else//03 04功能码读取
    {
        int TmpRegister=0;
        int index=0;
        _HexToFloat HexToFloat;
        //qDebug()<<endl;
        for(int i=0;i<m_Data.count();i++)
        {
            TmpRegister=m_Data[i]._Regisetr_U*256+m_Data[i]._Regisetr_L;
            index=(TmpRegister-m_begRegigter)*2+3;//确定数据从第几个字节开始,加个3偏移地址功，能码，长度字节
            switch (m_Data[i].type)
            {
            case _REGISTER_TYPE::_UINT16:
                m_Data[i].m_uint16=(CurrByte[index+m_PInt16[0]]<<8)+CurrByte[index+m_PInt16[1]];
                //qDebug()<<"================m_uint16"<<m_Data[i].m_uint16;
                break;
            case _REGISTER_TYPE::_UINT32:
                m_Data[i].m_uint32=(CurrByte[index+m_PInt32[0]]<<24)+(CurrByte[index+m_PInt32[1]]<<16)+(CurrByte[index+m_PInt32[2]]<<8)+CurrByte[index+m_PInt32[3]];
                //qDebug()<<"================m_uint32"<<m_Data[i].m_uint32;
                break;
            case _REGISTER_TYPE::_INT16:
                m_Data[i].m_int16=(CurrByte[index+m_PInt16[0]]<<8)+CurrByte[index+m_PInt16[1]];
                //qDebug()<<"================m_int16"<<m_Data[i].m_int16;
                break;
            case _REGISTER_TYPE::_INT32:
                m_Data[i].m_int32=(CurrByte[index+m_PInt32[0]]<<24)+(CurrByte[index+m_PInt32[1]]<<16)+(CurrByte[index+m_PInt32[2]]<<8)+CurrByte[index+m_PInt32[3]];
                //qDebug()<<"================m_int32"<<m_Data[i].m_int32;
                break;
            case _REGISTER_TYPE::_FLOAT32:
                HexToFloat.hex[3]=CurrByte[index+m_PFloat[0]];
                HexToFloat.hex[2]=CurrByte[index+m_PFloat[1]];
                HexToFloat.hex[1]=CurrByte[index+m_PFloat[2]];
                HexToFloat.hex[0]=CurrByte[index+m_PFloat[3]];
                m_Data[i].m_float=HexToFloat.dataf;
                //qDebug()<<"================m_float"<<m_Data[i].m_float;
                break;
            default:
                break;
            }

        }
    }
    return 0;
}

int ModbusMaster::RespondByteIsValid(QByteArray &RetByte,_FUNCTION_CODE FuntionCode)
{
    if(RetByte.count()==0){ return -4; }
    if(m_Data.count()==0){ return -4; }
    _MODBUS_READ_RESPOND Respond;
    int RecLen=0;//实际返回值
    if(m_MbType==_MODBUS_TYPE::_MODBUSRTU)
    {
        if(!CheckCRC_16_IsOk(RetByte,0,RetByte.count()))
        {
            return -1;
        }
        RecLen=RetByte.count()-2;
    }
    else
    {
        RetByte=RetByte.mid(5,-1);
        RecLen=RetByte.count();
        Respond._TCPDataLen=RetByte[5];
    }
    Respond._DevAddr=RetByte[0];
    unsigned char code=(unsigned char)RetByte[1];
    Respond._FunCode=(_FUNCTION_CODE)code;
    Respond._DataLen=RetByte[2];

    if(Respond._FunCode!=FuntionCode){ return -3; }
    if(Respond._DevAddr!=m_Addr){ return -2; }

    int RespondLen=0;//实际应该返回长度，没有加校验两个字节
    int yushu=0;
    if(FuntionCode==_FUNCTION_CODE::_CODE_01||FuntionCode==_FUNCTION_CODE::_CODE_02)
    {
        yushu=m_registerCoutn%8;
        RespondLen=m_registerCoutn/8+3;
        if(yushu!=0){ RespondLen+=1; }
    }else if(FuntionCode==_FUNCTION_CODE::_CODE_03||FuntionCode==_FUNCTION_CODE::_CODE_04)
    {
        RespondLen=m_registerCoutn*2+3;
    }else if(FuntionCode==_FUNCTION_CODE::_CODE_05||FuntionCode==_FUNCTION_CODE::_CODE_0F)
    {
        RespondLen=6;
    }else if(FuntionCode==_FUNCTION_CODE::_CODE_06||FuntionCode==_FUNCTION_CODE::_CODE_10)
    {
        RespondLen=6;
    }
    //qDebug()<<"RecLen"<<RecLen<<"RespondLen"<<RespondLen;
    if(RecLen!=RespondLen){ return -4; }
    return 0;
}

QByteArray ModbusMaster::WriteDataToByte_05(const int index, int &ret)
{
    if(index<0||index>=m_Data.count()){
        ret=-2;
        return QByteArray();
    }
    return WriteDataToByte_05(m_Data[index],ret);
}

QByteArray ModbusMaster::WriteDataToByte_05(const _MODBUS_DATA &data, int &ret)
{
    if(data.type!=_REGISTER_TYPE::_BOOL){
        ret=-1;
        return QByteArray();
    }
    if(m_MbCode!=_FUNCTION_CODE::_CODE_01){
        ret=-3;
        return QByteArray();
    }
    QByteArray RetByte;
    if(m_MbType==_MODBUS_TYPE::_MODBUSRTU)
    {
        RetByte.append(m_Addr);
    }
    else if(m_MbType==_MODBUS_TYPE::_MODBUSTCP)
    {
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x06));
        RetByte.append(m_Addr);
    }
    RetByte.append(char(0x05));
    RetByte.append(char(data._Regisetr_U));
    RetByte.append(char(data._Regisetr_L));
    if(data.m_bool==true){
        RetByte.append(char(0xFF));
    }else{
        RetByte.append(char(0x00));
    }
    RetByte.append(char(0x00));
    if(m_MbType==_MODBUS_TYPE::_MODBUSRTU){
        CheckCRC_16_add(RetByte,0,RetByte.count());
    }
    ret=0;
    return RetByte;
}

int ModbusMaster::RespondByteWrite_05(QByteArray &RespondByte, const QByteArray &SendByte)
{
    int ret=RespondByteIsValid(RespondByte,_FUNCTION_CODE::_CODE_05);
    if(ret!=0){ return ret; }

    if(RespondByte[2]==SendByte[2]&&RespondByte[3]==SendByte[3]&&RespondByte[4]==SendByte[4]&&RespondByte[5]==SendByte[5])
    {
        return 0;
    }else{
        return -5;
    }
}

QByteArray ModbusMaster::WriteDataToByte_0F(int &ret)
{
    if(m_MbCode!=_FUNCTION_CODE::_CODE_01){
        ret=-3;
        return QByteArray();
    }
    int yushu=0;
    yushu=m_registerCoutn%8;
    int sendLen=m_registerCoutn/8;
    if(yushu!=0){ sendLen+=1; }

    QByteArray RetByte;
    if(m_MbType==_MODBUS_TYPE::_MODBUSTCP)
    {
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x06));
    }
    RetByte.append(char(m_Addr));
    RetByte.append(char(0x0F));
    RetByte.append(char((m_begRegigter&0xFF00)>>8));
    RetByte.append(char(m_begRegigter&0x00FF));
    RetByte.append(char((m_registerCoutn&0xFF00)>>8));
    RetByte.append(char(m_registerCoutn&0x00FF));
    RetByte.append(char(sendLen));
    for(int i=0;i<sendLen;i++)
    {
        RetByte.append(char(0x00));
    }
    int TmpRegister=0;
    int index=0;
    char tmpValue=0;
    for(int i=0;i<m_Data.count();i++)
    {
        TmpRegister=m_Data[i]._Regisetr_U*256+m_Data[i]._Regisetr_L;
        index=(TmpRegister-m_begRegigter)/8+1;//确定数据在第几个字节
        yushu=(TmpRegister-m_begRegigter)%8;//确定数据在第几位
        if(index>sendLen){ continue; }
        //加个6偏移地址功，能码，长度字节
        tmpValue=0x00;
        if(m_Data[i].m_bool)
        {
            tmpValue=(1<<yushu);//如果为true，就把1移位到对应位置，否则该位保持0；
        }
        RetByte[index+6]=RetByte[index+6]|tmpValue;
    }
    if(m_MbType==_MODBUS_TYPE::_MODBUSRTU){
        CheckCRC_16_add(RetByte,0,RetByte.count());
    }
    ret=0;
    return RetByte;
}

int ModbusMaster::RespondByteWrite_0F(QByteArray &RespondByte, const QByteArray &SendByte)
{
    int ret=RespondByteIsValid(RespondByte,_FUNCTION_CODE::_CODE_0F);
    if(ret!=0){ return ret; }

    if(RespondByte[2]==SendByte[2]&&RespondByte[3]==SendByte[3]&&RespondByte[4]==SendByte[4]&&RespondByte[5]==SendByte[5]){
        return 0;
    }else{
        return -5;
    }
}

void ModbusMaster::setModbusAddr(const int addrValue)
{
    m_Addr=addrValue;
}

QByteArray ModbusMaster::WriteDataToByte_06(const int index, int &ret)
{
    if(index<0||index>=m_Data.count()){
        ret=-2;
        return QByteArray();
    }
    return WriteDataToByte_06(m_Data[index],ret);
}

QByteArray ModbusMaster::WriteDataToByte_06(const _MODBUS_DATA &data, int &ret)
{
    if(data.type==_REGISTER_TYPE::_INT16||data.type==_REGISTER_TYPE::_UINT16){

    }else{
        ret=-1;
        return QByteArray();
    }
    if(m_MbCode!=_FUNCTION_CODE::_CODE_03){
        ret=-3;
        return QByteArray();
    }
    QByteArray RetByte;
    if(m_MbType==_MODBUS_TYPE::_MODBUSTCP)
    {
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x06));
    }
    RetByte.append(char(m_Addr));
    RetByte.append(char(0x06));
    RetByte.append(char(data._Regisetr_U));
    RetByte.append(char(data._Regisetr_L));

    unsigned char TmpData[2];
    if(data.type==_REGISTER_TYPE::_INT16){
        TmpData[0]=(unsigned char)((data.m_int16&0xFF00)>>8);
        TmpData[1]=(unsigned char)(data.m_int16&0x00FF);
    }else if(data.type==_REGISTER_TYPE::_UINT16){
        TmpData[0]=(unsigned char)((data.m_uint16&0xFF00)>>8);
        TmpData[1]=(unsigned char)(data.m_uint16&0x00FF);
    }
    RetByte.append(TmpData[m_PInt16[0]]);
    RetByte.append(TmpData[m_PInt16[1]]);
    if(m_MbType==_MODBUS_TYPE::_MODBUSRTU){
        CheckCRC_16_add(RetByte,0,RetByte.count());
    }
    ret=0;
    return RetByte;
}

int ModbusMaster::RespondByteWrite_06(QByteArray &RespondByte, const QByteArray &SendByte)
{
    int ret=RespondByteIsValid(RespondByte,_FUNCTION_CODE::_CODE_06);
    if(ret!=0){ return ret; }

    if(RespondByte[2]==SendByte[2]&&RespondByte[3]==SendByte[3]&&RespondByte[4]==SendByte[4]&&RespondByte[5]==SendByte[5])
    {
        return 0;
    }else{
        return -5;
    }
}

QByteArray ModbusMaster::WriteDataToByte_10(const int index, int &ret)
{
    if(index<0||index>=m_Data.count()){
        ret=-2;
        return QByteArray();
    }
    return WriteDataToByte_10(m_Data[index],ret);
}

QByteArray ModbusMaster::WriteDataToByte_10(const _MODBUS_DATA &data, int &ret)
{
    if(data.type==_REGISTER_TYPE::_INT32||data.type==_REGISTER_TYPE::_UINT32||data.type==_REGISTER_TYPE::_FLOAT32){

    }else{
        ret=-1;
        return QByteArray();
    }
    if(m_MbCode!=_FUNCTION_CODE::_CODE_03){
        ret=-3;
        return QByteArray();
    }
    QByteArray RetByte;
    if(m_MbType==_MODBUS_TYPE::_MODBUSTCP)
    {
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x00));
        RetByte.append(char(0x06));
    }
    RetByte.append(char(m_Addr));
    RetByte.append(char(0x10));
    RetByte.append(char(data._Regisetr_U));
    RetByte.append(char(data._Regisetr_L));
    RetByte.append(char(0x00));
    RetByte.append(char(0x02));
    RetByte.append(char(0x04));

    _HexToFloat HexToFloat;
    unsigned char TmpData[4];
    if(data.type==_REGISTER_TYPE::_INT32){
        TmpData[0]=(unsigned char)((data.m_int32&0xFF00)>>24);
        TmpData[1]=(unsigned char)((data.m_int32&0xFF00)>>16);
        TmpData[2]=(unsigned char)((data.m_int32&0xFF00)>>8);
        TmpData[3]=(unsigned char)(data.m_int32&0x00FF);
    }else if(data.type==_REGISTER_TYPE::_UINT32){
        TmpData[0]=(unsigned char)((data.m_uint32&0xFF00)>>24);
        TmpData[1]=(unsigned char)((data.m_uint32&0xFF00)>>16);
        TmpData[2]=(unsigned char)((data.m_uint32&0xFF00)>>8);
        TmpData[3]=(unsigned char)(data.m_uint32&0x00FF);
    }else if(data.type==_REGISTER_TYPE::_FLOAT32){
        HexToFloat.dataf=data.m_float;
        TmpData[0]=HexToFloat.hex[3];
        TmpData[1]=HexToFloat.hex[2];
        TmpData[2]=HexToFloat.hex[1];
        TmpData[3]=HexToFloat.hex[0];
    }
    if(data.type==_REGISTER_TYPE::_FLOAT32){
        RetByte.append(TmpData[m_PFloat[0]]);
        RetByte.append(TmpData[m_PFloat[1]]);
        RetByte.append(TmpData[m_PFloat[2]]);
        RetByte.append(TmpData[m_PFloat[3]]);
    }else{
        RetByte.append(TmpData[m_PInt32[0]]);
        RetByte.append(TmpData[m_PInt32[1]]);
        RetByte.append(TmpData[m_PInt32[2]]);
        RetByte.append(TmpData[m_PInt32[3]]);
    }

    if(m_MbType==_MODBUS_TYPE::_MODBUSRTU){
        CheckCRC_16_add(RetByte,0,RetByte.count());
    }
    ret=0;
    return RetByte;
}

int ModbusMaster::RespondByteWrite_10(QByteArray &RespondByte, const QByteArray &SendByte)
{
    int ret=RespondByteIsValid(RespondByte,_FUNCTION_CODE::_CODE_10);
    if(ret!=0){ return ret; }

    if(RespondByte[2]==SendByte[2]&&RespondByte[3]==SendByte[3]&&RespondByte[4]==SendByte[4]&&RespondByte[5]==SendByte[5])
    {
        return 0;
    }else{
        return -5;
    }
}

int ModbusMaster::getModbusAddr()
{
    return m_Addr;
}

void ModbusMaster::appendData(const _MODBUS_DATA &data)
{
    int lastReg=0;
    int tmpRegister=0;
    tmpRegister=data._Regisetr_U*256+data._Regisetr_L;
    if(m_begRegigter>tmpRegister)
    {
        m_begRegigter=tmpRegister;
    }
    if(m_endRegigter<tmpRegister)
    {
        m_endRegigter=tmpRegister;
        //为了判断总的寄存器，需要判断最大的寄存器地址的数据类型类确定寄存器个数
        switch (data.type)
        {
        case _REGISTER_TYPE::_BOOL:
            lastReg=0;
            break;
        case _REGISTER_TYPE::_UINT16:
            lastReg=0;
            break;
        case _REGISTER_TYPE::_UINT32:
            lastReg=1;
            break;
        case _REGISTER_TYPE::_INT16:
            lastReg=0;
            break;
        case _REGISTER_TYPE::_INT32:
            lastReg=1;
            break;
        case _REGISTER_TYPE::_FLOAT32:
            lastReg=1;
            break;
        default:
            break;
        }
    }

    m_Data.append(data);
    m_registerCoutn=m_endRegigter-m_begRegigter+lastReg+1;
    //qDebug()<<"m_registerCoutn"<<m_registerCoutn<<lastReg<<data.type;
}

void ModbusMaster::appendData(const QVector<_MODBUS_DATA> &data)
{
    for(int i=0;i<data.count();i++){
        this->appendData(data[i]);
    }
}

QVector<_MODBUS_DATA> &ModbusMaster::data()
{
    return m_Data;
}

QVector<_MODBUS_DATA> ModbusMaster::getDataVector()
{
    return m_Data;
}

void ModbusMaster::clenData()
{
    m_begRegigter=0x0000FFFF;
    m_endRegigter=0;
    m_registerCoutn=0;
    m_Data.clear();
}

void ModbusMaster::setParseInt16(int a, int b)
{
    if(a>1){ a=1; }
    if(a<0){ a=0; }
    if(b>1){ b=1; }
    if(b<0){ b=0; }

    m_PInt16[0]=a;
    m_PInt16[1]=b;
}

void ModbusMaster::setParseInt32(int a, int b, int c, int d)
{
    if(a>3){ a=3; }
    if(a<0){ a=0; }
    if(b>3){ b=3; }
    if(b<0){ b=0; }
    if(c>3){ c=3; }
    if(c<0){ c=0; }
    if(d>3){ d=3; }
    if(d<0){ d=0; }

    m_PInt32[0]=a;
    m_PInt32[1]=b;
    m_PInt32[2]=c;
    m_PInt32[3]=d;
}

void ModbusMaster::setParseFloat(int a, int b, int c, int d)
{
    if(a>3){ a=3; }
    if(a<0){ a=0; }
    if(b>3){ b=3; }
    if(b<0){ b=0; }
    if(c>3){ c=3; }
    if(c<0){ c=0; }
    if(d>3){ d=3; }
    if(d<0){ d=0; }

    m_PFloat[0]=a;
    m_PFloat[1]=b;
    m_PFloat[2]=c;
    m_PFloat[3]=d;
}
